from google.appengine.ext import webapp
from google.appengine.ext import blobstore
from google.appengine.ext import db

from datatypes import Asset
from utils import *
from CONFIG import *

"""
Handles a requests for /delete?id=<resource> and
removes the blob from the Blobstore/deletes the
Asset object.
"""
class DeleteHandler(webapp.RequestHandler):
    
    @login_required
    @admin_required
    def get(self):
        # Get the blob
        resource = self.request.get("id")
        blob_info = blobstore.BlobInfo.get(resource)

        # Delete the Asset object
        results = db.GqlQuery("SELECT * FROM Asset WHERE asset_key = :key", key = blob_info.key())
        if results.count() > 0:
            results[0].delete()
            
            # Delete the blob
            blob_info.delete()

        # Redirect to management console
        self.redirect(GAPYCDN_CONFIG["base_url"] + "manage")
